package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private JwtProperties jwtProperties;

    @PutMapping
    public Result<String > update(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类：{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO   categoryPageQueryDTO) {
        log.info("分类分页查询：{}",  categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery( categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable   Integer status, Long id) {
        log.info("{}分类：{}", status == 1 ? "启" : "禁", id);
        categoryService.startOrStop(status, id);
        return Result.success();
    }


    @PostMapping
    public Result<String > save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result  delete(Long id) {
        log.info("删除分类：{}", id);
        categoryService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        log.info("查询分类：{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}
